package org.gdstash.ui.character;

import org.gdstash.character.GDChar;
import org.gdstash.character.GDCharTeleportList;
import org.gdstash.character.GDCharUID;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.GDAbstractRiftPane;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;

public class RoTRiftAct2Pane extends GDAbstractRiftPane {
    private JCheckBox cbLutGholein;
    private JCheckBox cbSewers;
    private JCheckBox cbDryHills;
    private JCheckBox cbHallsDead;
    private JCheckBox cbFarOasis;
    private JCheckBox cbLostCity;
    private JCheckBox cbPalaceCellar;
    private JCheckBox cbArcaneSanctuary;
    private JCheckBox cbCanyonMagi;
    private int difficulty;
    private boolean changed;

    public RoTRiftAct2Pane(final int difficulty, final int direction) {
        this.difficulty = difficulty;
        this.changed = false;
        this.adjustUI();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.cbLutGholein).addComponent(this.cbSewers).addComponent(this.cbDryHills).addComponent(this.cbHallsDead).addComponent(this.cbFarOasis).addComponent(this.cbLostCity).addComponent(this.cbPalaceCellar).addComponent(this.cbArcaneSanctuary).addComponent(this.cbCanyonMagi));
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.cbLutGholein)).addGroup(layout.createParallelGroup().addComponent(this.cbSewers)).addGroup(layout.createParallelGroup().addComponent(this.cbDryHills)).addGroup(layout.createParallelGroup().addComponent(this.cbHallsDead)).addGroup(layout.createParallelGroup().addComponent(this.cbFarOasis)).addGroup(layout.createParallelGroup().addComponent(this.cbLostCity)).addGroup(layout.createParallelGroup().addComponent(this.cbPalaceCellar)).addGroup(layout.createParallelGroup().addComponent(this.cbArcaneSanctuary)).addGroup(layout.createParallelGroup().addComponent(this.cbCanyonMagi));
        if (direction == 0) {
            layout.setHorizontalGroup(vGroup);
            layout.setVerticalGroup(hGroup);
        } else {
            layout.setHorizontalGroup(hGroup);
            layout.setVerticalGroup(vGroup);
        }
        layout.linkSize(0, this.cbLutGholein, this.cbSewers);
        layout.linkSize(0, this.cbLutGholein, this.cbDryHills);
        layout.linkSize(0, this.cbLutGholein, this.cbHallsDead);
        layout.linkSize(0, this.cbLutGholein, this.cbFarOasis);
        layout.linkSize(0, this.cbLutGholein, this.cbLostCity);
        layout.linkSize(0, this.cbLutGholein, this.cbPalaceCellar);
        layout.linkSize(0, this.cbLutGholein, this.cbArcaneSanctuary);
        layout.linkSize(0, this.cbLutGholein, this.cbCanyonMagi);
        layout.linkSize(1, this.cbLutGholein, this.cbSewers);
        layout.linkSize(1, this.cbLutGholein, this.cbDryHills);
        layout.linkSize(1, this.cbLutGholein, this.cbHallsDead);
        layout.linkSize(1, this.cbLutGholein, this.cbFarOasis);
        layout.linkSize(1, this.cbLutGholein, this.cbLostCity);
        layout.linkSize(1, this.cbLutGholein, this.cbPalaceCellar);
        layout.linkSize(1, this.cbLutGholein, this.cbArcaneSanctuary);
        layout.linkSize(1, this.cbLutGholein, this.cbCanyonMagi);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntCheck = UIManager.getDefaults().getFont("CheckBox.font");
        if (fntCheck == null) {
            fntCheck = fntLabel;
        }
        Font fntBorder = UIManager.getDefaults().getFont("TitledBorder.font");
        if (fntBorder == null) {
            fntBorder = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntCheck = fntCheck.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntBorder = fntBorder.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        final TitledBorder text = BorderFactory.createTitledBorder(compound, GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ACT2"));
        text.setTitleFont(fntBorder);
        this.setBorder(text);
        if (this.cbLutGholein == null) {
            (this.cbLutGholein = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbLutGholein.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_02_LUT_GHOLEIN"));
        this.cbLutGholein.setFont(fntCheck);
        if (this.cbSewers == null) {
            (this.cbSewers = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbSewers.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_02_SEWERS"));
        this.cbSewers.setFont(fntCheck);
        if (this.cbDryHills == null) {
            (this.cbDryHills = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbDryHills.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_02_DRY_HILLS"));
        this.cbDryHills.setFont(fntCheck);
        if (this.cbHallsDead == null) {
            (this.cbHallsDead = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbHallsDead.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_02_HALLS_DEAD"));
        this.cbHallsDead.setFont(fntCheck);
        if (this.cbFarOasis == null) {
            (this.cbFarOasis = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbFarOasis.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_02_FAR_OASIS"));
        this.cbFarOasis.setFont(fntCheck);
        if (this.cbLostCity == null) {
            (this.cbLostCity = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbLostCity.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_02_LOST_CITY"));
        this.cbLostCity.setFont(fntCheck);
        if (this.cbPalaceCellar == null) {
            (this.cbPalaceCellar = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbPalaceCellar.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_02_PALACE_CELLAR"));
        this.cbPalaceCellar.setFont(fntCheck);
        if (this.cbArcaneSanctuary == null) {
            (this.cbArcaneSanctuary = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbArcaneSanctuary.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_02_ARCANE_SANCTUARY"));
        this.cbArcaneSanctuary.setFont(fntCheck);
        if (this.cbCanyonMagi == null) {
            (this.cbCanyonMagi = new JCheckBox()).addActionListener(new ChangeActionListener());
        }
        this.cbCanyonMagi.setText(GDMsgFormatter.getString(GDMsgFormatter.rbGD, "TXT_RIFT_ROT_02_CANYON_MAGI"));
        this.cbCanyonMagi.setFont(fntCheck);
    }

    @Override
    public boolean hasChanged() {
        return this.changed;
    }

    @Override
    public void setChanged(final boolean changed) {
        this.changed = changed;
    }

    public void setSelected(final boolean selected) {
        this.cbLutGholein.setSelected(selected);
        this.cbSewers.setSelected(selected);
        this.cbDryHills.setSelected(selected);
        this.cbHallsDead.setSelected(selected);
        this.cbFarOasis.setSelected(selected);
        this.cbLostCity.setSelected(selected);
        this.cbPalaceCellar.setSelected(selected);
        this.cbArcaneSanctuary.setSelected(selected);
        this.cbCanyonMagi.setSelected(selected);
    }

    @Override
    public List<GDCharUID> getRiftList(final boolean found) {
        final List<GDCharUID> list = new LinkedList<GDCharUID>();
        if (this.cbLutGholein.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_02_LUT_GHOLEIN);
        }
        if (this.cbSewers.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_02_SEWERS);
        }
        if (this.cbDryHills.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_02_DRY_HILLS);
        }
        if (this.cbHallsDead.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_02_HALLS_DEAD);
        }
        if (this.cbFarOasis.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_02_FAR_OASIS);
        }
        if (this.cbLostCity.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_02_LOST_CITY);
        }
        if (this.cbPalaceCellar.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_02_PALACE_CELLAR);
        }
        if (this.cbArcaneSanctuary.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_02_ARCANE_SANCTUARY);
        }
        if (this.cbCanyonMagi.isSelected() == found) {
            list.add(GDCharTeleportList.UID_RIFT_ROT_02_CANYON_MAGI);
        }
        return list;
    }

    @Override
    public void setChar(final GDChar gdc) {
        this.setSelected(false);
        this.changed = false;
        if (gdc == null) {
            return;
        }
        final List<GDCharUID> list = gdc.getRiftList(this.difficulty);
        for (final GDCharUID uid : list) {
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_02_LUT_GHOLEIN)) {
                this.cbLutGholein.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_02_SEWERS)) {
                this.cbSewers.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_02_DRY_HILLS)) {
                this.cbDryHills.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_02_HALLS_DEAD)) {
                this.cbHallsDead.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_02_FAR_OASIS)) {
                this.cbFarOasis.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_02_LOST_CITY)) {
                this.cbLostCity.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_02_PALACE_CELLAR)) {
                this.cbPalaceCellar.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_02_ARCANE_SANCTUARY)) {
                this.cbArcaneSanctuary.setSelected(true);
            }
            if (uid.equals(GDCharTeleportList.UID_RIFT_ROT_02_CANYON_MAGI)) {
                this.cbCanyonMagi.setSelected(true);
            }
        }
    }

    private class ChangeActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            RoTRiftAct2Pane.this.changed = true;
        }
    }
}
